<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/04/30 10:21:51 $)
// $RCSfile: staff_predefined.php,v $ : $Revision: 1.10 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["t_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./includes/functions_html.php");
require_once ("./includes/functions_xml.php");
require_once ("./includes/functions_jscache.php");
require_once ("./modules/tickets/functions_predefined.php");
$grid = new Grid();
$template->loadLanguageSection("staff_tickets");
$template->loadLanguageSection("predefined");

$interface->assignQuickLink($_SWIFT["language"]["managecategories"], "index.php?_m=tickets&_a=managepredcat", "icon_list.gif");
$interface->assignQuickLink($_SWIFT["language"]["managereplies"], "index.php?_m=tickets&_a=managepredreplies", "icon_topicyellow.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertcategory"], "index.php?_m=tickets&_a=insertpredcat", "icon_newcategory.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertreply"], "index.php?_m=tickets&_a=insertpredreply", "icon_inserttopic.gif");


/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing replies
*/
function _replyFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

//	$record["predefinedcategories.categorytype"] = iif($record["categorytype"]==CATEGORY_PUBLIC||empty($record["categorytype"]), '<img src="'. $_SWIFT["themepath"] .'icon_public.gif" align="absmiddle" border="0" />&nbsp;'.$_SWIFT["language"]["public"], '<img src="'. $_SWIFT["themepath"] .'icon_private.gif" align="absmiddle" border="0" />&nbsp;'.$_SWIFT["language"]["private"]);
	$record["predefinedreplies.subject"] = '<a href="index.php?_m=tickets&_a=editpredreply&predefinedreplyid='. $record["predefinedreplyid"] .'" title="'. $_SWIFT["language"]["edit"] .'">'.htmlspecialchars($record["subject"]).'</a>';
	$record["predefinedreplies.dateline"] = edate($_SWIFT["settings"]["dt_datetimeformat"], $record["dateline"]);
	$record["predefinedcategories.title"] = iif(empty($record["title"]), $_SWIFT["language"]["parentcategory"], $record["title"]);

	$record["options"] = '<a href="index.php?_m=tickets&_a=editpredreply&predefinedreplyid='. $record["predefinedreplyid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=tickets&_a=managepredreplies&do=delete&predefinedreplyid='. $record["predefinedreplyid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';
	return $record;
}

/**
* Mass Action: Delete Predefined Replies
*/
function _maDeleteReplies()
{
	global $_SWIFT;

	if (!is_array($_POST["itemid"]))
	{
		return false;
	}

	deletePredefinedReplies($_POST["itemid"]);
}



/**
* ###############################################
* MANAGE PREDEFINED CATEGORIES
* ###############################################
*/
if ($eventaction == "managepredcat")
{
	if ($_GET["do"] == "delete" && $_GET["predefinedcategoryid"] != "")
	{
		deleteCategory($_GET["predefinedcategoryid"]);

		printRedirect($_SWIFT["language"]["catdelconfirm"], "index.php?_m=tickets&_a=managepredcat&categorydel=1");
		exit;
	}

	if (trim($_REQUEST["newcatinsert"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["catinsertconfirm"], htmlspecialchars($_REQUEST["newcatinsert"]));
	} else if ($_REQUEST["categorydel"] == 1) {
		$infomessage = $_SWIFT["language"]["catdelconfirm"];
	} else if (trim($_REQUEST["catupdate"]) != "") {
		$infomessage = sprintf($_SWIFT["language"]["catupdateconfirm"], htmlspecialchars($_REQUEST["catupdate"]));
	}
	
	require_once ("./includes/TreeMenu/TreeMenu.php");
	$template->assign("treemenu", true);

	$interface->staffHeader($_SWIFT["language"]["predefined"] ." &gt; ".$_SWIFT["language"]["managecategories"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=managepredreplies" title="'.$_SWIFT["language"]["predefined"].'">'.$_SWIFT["language"]["predefined"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managepredcat" title="'.$_SWIFT["language"]["managecategories"].'">'.$_SWIFT["language"]["managecategories"]."</a>", "", 2);

	printInfoBox($infomessage);
	printErrorBox($errormessage);
	
	$icon = 'icon_folderyellow3.gif';
	$expandedIcon = 'icon_folderyellow3.gif';

	$menu = new HTML_TreeMenu();

	$node1 = new HTML_TreeNode(array('text' => "&nbsp;". $_SWIFT["language"]["parentcategory"] ."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ <a href='index.php?_m=tickets&_a=insertpredcat&parentcategoryid=0' id='blue' title='". $_SWIFT["language"]["addsubcat"] ."'>". $_SWIFT["language"]["addsubcat"] ."</a> | <a href='index.php?_m=tickets&_a=insertpredreply&parentcategoryid=0' id='blue' title='". $_SWIFT["language"]["addreply"] ."'>". $_SWIFT["language"]["addreply"] ."</a> ]", 'link' => "", 'icon' => "icon_list.gif", 'expandedIcon' => "icon_list.gif", 'expanded' => true));

	buildCategoryTreeData($node1);

	$menu->addItem($node1);
	
	// Create the presentation class
	$treeMenu = &new HTML_TreeMenu_DHTML($menu, array('images' => stripTrailingSlash($_SWIFT["themepath"]), 'defaultClass' => 'treeMenuDefault'));

	printMainTableHeader($_SWIFT["language"]["predefinedcategories"]);

	ob_start();
	$treeMenu->printMenu();
	$contents = ob_get_contents();
	ob_end_clean();

	printDescRow($contents, "2", "defaultrow");

	printMainTableFooter();

	$interface->staffFooter();











/**
* ###############################################
* MANAGE PREDEFINED REPLIES
* ###############################################
*/
} else if ($eventaction == "managepredreplies") {
	if ($_GET["do"] == "delete" && $_GET["predefinedreplyid"] != "")
	{
		$infomessage = $_SWIFT["language"]["replydelconfirm"];
		deletePredefinedReplies(array($_GET["predefinedreplyid"]));
	}

	if (trim($_REQUEST["newreplyinsert"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["replyinsertconfirm"], htmlspecialchars($_REQUEST["newreplyinsert"]));
	} else if (trim($_REQUEST["replyupdate"]) != "") {
		$infomessage = sprintf($_SWIFT["language"]["replyupdateconfirm"], htmlspecialchars($_REQUEST["replyupdate"]));
	}
	
	if(!empty($_GET["parentcategoryid"]))
	{
		$predefinedcategories = getSubCategoryList($_GET["parentcategoryid"], buildCategoryData(($_temp=array())), ($_temp = array()));
		$predefinedcategories[] = $_GET["parentcategoryid"];
	}else{
		$predefinedcategories = getSubCategoryList("0", buildCategoryData(($_temp=array())), ($_temp = array()));
		$predefinedcategories[] = 0;
	}

	$options["recordsperpage"] = "10";
	$options["sortby"] = "predefinedreplies.dateline";
	$options["sortorder"] = "desc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteReplies";

	$options["idname"] = "predefinedreplyid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["subject"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'predefinedreplies` AS predefinedreplies LEFT JOIN `'. TABLE_PREFIX .'predefinedcategories` AS predefinedcategories ON (predefinedreplies.predefinedcategoryid = predefinedcategories.predefinedcategoryid) WHERE predefinedreplies.subject LIKE \'%$_searchstr%\' AND predefinedreplies.predefinedcategoryid IN ('. buildIN($predefinedcategories) .') $_sortjoin;';
	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["contents"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'predefinedreplies` AS predefinedreplies LEFT JOIN `'. TABLE_PREFIX .'predefinedcategories` AS predefinedcategories ON (predefinedreplies.predefinedcategoryid = predefinedcategories.predefinedcategoryid) LEFT JOIN `'. TABLE_PREFIX .'predefinedreplydata` AS predefinedreplydata ON (predefinedreplies.predefinedreplyid = predefinedreplydata.predefinedreplyid) WHERE predefinedreplydata.contents LIKE \'%$_searchstr%\' AND predefinedreplies.predefinedcategoryid IN ('. buildIN($predefinedcategories) .') $_sortjoin;';

	$options["callback"] = "_replyFields";

	$fields[0]["name"] = "predefinedreplies.subject";
	$fields[0]["title"] = $_SWIFT["language"]["subject"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "predefinedcategories.title";
	$fields[1]["title"] = $_SWIFT["language"]["category"];
	$fields[1]["width"] = "130";
	$fields[1]["align"] = "center";

/*	$fields[2]["name"] = "predefinedcategories.categorytype";
	$fields[2]["title"] = $_SWIFT["language"]["replytype"];
	$fields[2]["width"] = "130";
	$fields[2]["align"] = "center";*/

	$fields[2]["name"] = "predefinedreplies.dateline";
	$fields[2]["title"] = $_SWIFT["language"]["createdon"];
	$fields[2]["width"] = "160";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "150";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->staffHeader($_SWIFT["language"]["predefined"] ." &gt; ".$_SWIFT["language"]["managereplies"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=managepredcat" title="'.$_SWIFT["language"]["predefined"].'">'.$_SWIFT["language"]["predefined"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managepredreplies" title="'.$_SWIFT["language"]["managereplies"].'">'.$_SWIFT["language"]["managereplies"]."</a>", "", 2);

	printErrorBox($errormessage);
	printInfoBox($infomessage);

	$grid->start("predefined", $_SWIFT["language"]["predefinedreplylist"], 'SELECT * FROM `'. TABLE_PREFIX .'predefinedreplies` AS predefinedreplies LEFT JOIN `'. TABLE_PREFIX .'predefinedcategories` AS predefinedcategories ON (predefinedreplies.predefinedcategoryid = predefinedcategories.predefinedcategoryid) WHERE predefinedreplies.predefinedcategoryid IN ('. buildIN($predefinedcategories) .') $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'predefinedreplies` AS predefinedreplies WHERE predefinedreplies.predefinedcategoryid IN ('. buildIN($predefinedcategories) .');', 'SELECT * FROM `'. TABLE_PREFIX .'predefinedreplies` AS predefinedreplies LEFT JOIN `'. TABLE_PREFIX .'predefinedcategories` AS predefinedcategories ON (predefinedreplies.predefinedcategoryid = predefinedcategories.predefinedcategoryid) WHERE predefinedreplies.subject LIKE \'%$_searchstr%\' AND predefinedreplies.predefinedcategoryid IN ('. buildIN($predefinedcategories) .') $_sortjoin;', $fields, $options);
	
	$grid->display("predefined");

//	echo '<BR /><span class="smalltext">'. $_SWIFT["language"]["legend"] .'<img src="'. $_SWIFT["themepath"] .'icon_public.gif" border="0" /> '. $_SWIFT["language"]["publicreplies"] .'&nbsp;&nbsp;&nbsp; <img src="'. $_SWIFT["themepath"] .'icon_private.gif" border="0" /> '. $_SWIFT["language"]["privatereplies"].'</span>';

	$template->assign("backurl", "index.php?_m=tickets&_a=managepredcat");
	$interface->staffFooter();

/**
* ###############################################
* INSERT PREDEFINED CATEGORY
* ###############################################
*/
} else if ($eventaction == "insertpredcat") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			// Insert the new category
			insertPredefinedCategory($_POST["parentcategoryid"], $_POST["title"], CATEGORY_PUBLIC);

			printRedirect(sprintf($_SWIFT["language"]["catinsertconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=tickets&_a=managepredcat&newcatinsert=".urlencode($_POST["title"]));
			exit;
		}
	}

	$interface->staffHeader($_SWIFT["language"]["predefined"] ." &gt; ".$_SWIFT["language"]["insertcategory"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=managepredcat" title="'.$_SWIFT["language"]["predefined"].'">'.$_SWIFT["language"]["predefined"].'</a> &raquo; <a href="index.php?_m=tickets&_a=insertpredcat" title="'.$_SWIFT["language"]["insertcategory"].'">'.$_SWIFT["language"]["insertcategory"]."</a>", "", 2);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderCategoryForm(INSERT);

	$template->assign("backurl", "index.php?_m=tickets&_a=managepredcat");
	$interface->staffFooter();

/**
* ###############################################
* INSERT PREDEFINED REPLY
* ###############################################
*/
} else if ($eventaction == "insertpredreply") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["subject"]) == "" || trim($_POST["contents"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			insertPredefinedReply($_POST["parentcategoryid"], $_POST["subject"], $_POST["contents"]);

			if (!empty($_POST["ticketid"]) && $_POST["opt_addkb"] == 0)
			{
				// We need to redirect back to ticket listing.
				header("location: index.php?_m=tickets&_a=manage&departmentid=". intval($_POST["departmentid"]) ."&ticketstatusid=".intval($_POST["ticketstatusid"]));
				exit;
			} else if (!empty($_POST["ticketid"]) && $_POST["opt_addkb"] == 1) {
				// Redirect to kb
				header("location: index.php?_m=knowledgebase&_a=insertquestion&ticketid=". intval($_POST["ticketid"]) ."&ticketpostid=".intval($_POST["ticketpostid"])."&departmentid=".intval($_POST["departmentid"])."&ticketstatusid=".intval($_POST["ticketstatusid"]));
				exit;
			}
			printRedirect(sprintf($_SWIFT["language"]["replyinsertconfirm"], $_POST["subject"]), "index.php?_m=tickets&_a=managepredreplies&newreplyinsert=".urlencode($_POST["subject"]));
			exit;
		}
	}


	$interface->staffHeader($_SWIFT["language"]["predefined"] ." &gt; ".$_SWIFT["language"]["insertreply"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=managepredreplies" title="'.$_SWIFT["language"]["predefined"].'">'.$_SWIFT["language"]["predefined"].'</a> &raquo; <a href="index.php?_m=tickets&_a=insertpredreply" title="'.$_SWIFT["language"]["insertreply"].'">'.$_SWIFT["language"]["insertreply"]."</a>", "", 2);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderReplyForm(INSERT);

	$template->assign("backurl", "index.php?_m=tickets&_a=managepredreplies");
	$interface->staffFooter();






/**
* ###############################################
* EDIT PREDEFINED CATEGORY
* ###############################################
*/
} else if ($eventaction == "editpredcat") {
	$_category = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."predefinedcategories` WHERE `predefinedcategoryid` = '". intval($_REQUEST["predefinedcategoryid"]) ."';");
	if (empty($_category["predefinedcategoryid"]))
	{
		trigger_error($_SWIFT["language"]["invalidpredcat"], E_USER_ERROR);
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			// Update the category
			updatePredefinedCategory($_category["predefinedcategoryid"], $_POST["parentcategoryid"], $_POST["title"], CATEGORY_PUBLIC);

			printRedirect(sprintf($_SWIFT["language"]["catupdateconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=tickets&_a=managepredcat&catupdate=".urlencode($_POST["title"]));
			exit;
		}
	}

	$template->assign("itemoptiontitle", $_SWIFT["language"]["categoryoptions"]);
	$interface->assignItemOption($_SWIFT["language"]["deletecategory"], "", "icon_delete.gif", "", 'doConfirm(\''. $_SWIFT["language"]["catdelactionconfirm"] .'\', \'index.php?_m=tickets&_a=managepredcat&do=delete&predefinedcategoryid='. $_category["predefinedcategoryid"] .'\')');

	$_POST = array_merge($_POST, $_category);

	$interface->staffHeader($_SWIFT["language"]["predefined"] ." &gt; ".$_SWIFT["language"]["editcategory"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=managepredcat" title="'.$_SWIFT["language"]["predefined"].'">'.$_SWIFT["language"]["predefined"].'</a> &raquo; <a href="index.php?_m=tickets&_a=editpredcat&predefinedcategoryid='. intval($_category["predefinedcategoryid"]) .'" title="'.$_SWIFT["language"]["editcategory"].'">'.$_SWIFT["language"]["editcategory"]."</a>", "", 2);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderCategoryForm(EDIT);

	$template->assign("backurl", "index.php?_m=tickets&_a=managepredcat");
	$interface->staffFooter();








/**
* ###############################################
* EDIT PREDEFINED REPLY
* ###############################################
*/
} else if ($eventaction == "editpredreply") {
	$_reply = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."predefinedreplies` AS predefinedreplies LEFT JOIN `". TABLE_PREFIX ."predefinedreplydata` AS predefinedreplydata ON (predefinedreplies.predefinedreplyid = predefinedreplydata.predefinedreplyid) WHERE predefinedreplies.predefinedreplyid = '". intval($_REQUEST["predefinedreplyid"]) ."';");
	if (empty($_reply["predefinedreplyid"]))
	{
		trigger_error($_SWIFT["language"]["invalidpredefinedreply"], E_USER_ERROR);
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["subject"]) == "" || trim($_POST["contents"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			updatePredefinedReply($_reply["predefinedreplyid"], $_POST["parentcategoryid"], $_POST["subject"], $_POST["contents"]);

			printRedirect(sprintf($_SWIFT["language"]["replyupdateconfirm"], $_POST["subject"]), "index.php?_m=tickets&_a=managepredreplies&replyupdate=".urlencode($_POST["subject"]));
			exit;
		}
	}

	$template->assign("itemoptiontitle", $_SWIFT["language"]["replyoptions"]);
	$interface->assignItemOption($_SWIFT["language"]["deletereply"], "", "icon_delete.gif", "", 'doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=tickets&_a=managepredreplies&do=delete&predefinedreplyid='. $_reply["predefinedreplyid"] .'\')');

	$_POST = array_merge($_POST, $_reply);
	$_REQUEST["parentcategoryid"] = $_reply["predefinedcategoryid"];

	$interface->staffHeader($_SWIFT["language"]["predefined"] ." &gt; ".$_SWIFT["language"]["editreply"], 2);

	$interface->staffNavBar('<a href="index.php?_m=tickets&_a=managepredreplies" title="'.$_SWIFT["language"]["predefined"].'">'.$_SWIFT["language"]["predefined"].'</a> &raquo; <a href="index.php?_m=tickets&_a=editpredreply&predefinedreplyid='. intval($_reply["predefinedreplyid"]) .'" title="'.$_SWIFT["language"]["editreply"].'">'.$_SWIFT["language"]["Editreply"]."</a>", "", 2);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderReplyForm(EDIT);

	$template->assign("backurl", "index.php?_m=tickets&_a=managepredreplies");
	$interface->staffFooter();
}





?>